<?php
/**
 * Created by PhpStorm.
 * User: 68008000
 * Date: 2017/11/21
 * Time: 10:02
 */

namespace app\common\model;


use think\Model;

/**
 * Class Type
 * @package app\common\model
 * 类型模型
 */
class Type extends Model
{
    //连接数据库
    protected $table= 'shop_type';
    protected $pk= 'tid';
    //类型添加、编辑
    public function add($data){
        $tid=input('param.tid');
        if(isset($tid)){
            //编辑
            $res = $this->validate(true)->save($data,['tid' => $tid]);
            if(false === $res){
                // 验证失败 输出错误信息
                return ['valid'=>0,'msg'=>'修改失败'];
            }else{

                return ['valid'=>1,'msg'=>'修改成功'];
            }
        }else{
            //添加
        $result=$this->validate(true)->save($data);
        if(false == $result){
            return['valid'=>0,'msg'=>$this->getError()];
        }else{
            return['valid'=>1,'msg'=>'添加成功！'];
        }
    }
    }
    //删除
    public function del($tid){
        //1.通过当前要删除的数据的tid获取当前数据的shop_type_tid
        db('type-attribute')->where('shop_type_tid',$tid)->select();
        $res =db('type')->delete($tid);
        if ($res){
            db('type-attribute')->where('shop_type_tid',$tid)->delete();
            return ['valid'=>1,'msg'=>'删除成功'];
        }else{
            return ['valid'=>0,'msg'=>'删除失败'];
        }
    }
}